<?php
/*** 
 *
 *
 *
 **/

require_once '../db/db.php';
require_once '../util/perm.php';


// int -> int
function threadCount( $fid ) {
  if( !hasPerm('thread.view.count') ) 
    return MSG_UNAUTHORIZED;

  try {
    $db = openDbConn();
   
    $fid = intval((int)$fid);

    // XXX does this work? I don't member
    $res = $db->query("SELECT COUNT(id) AS tcount FROM thread WHERE fid = ${fid};")->fetch(); 
    $cnt = $res['tcount'];
    $res = NULL;
    return $cnt;
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}

// int -> obj?
// XXX clean up this uber messy query. Make it quicker mainly.
function getThread($tid) {
  if( !hasPerm('thread.view') ) 
    return MSG_UNAUTHORIZED;

  try {
    $db = openDbConn();
    $thread = $db->query("SELECT
                         thread.id AS id,
                         thread.fid AS fid,
                         thread.prio AS mode
                       FROM thread
                         WHERE thread.id = {$tid};");
    $data = $thread->fetch(PDO::FETCH_ASSOC);
    $thread = null;
    return $data;
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}

// int -> int -> int -> obj?
// XXX clean up this uber messy query. Make it quicker mainly.
function getThreads($fid, $start, $inc) {
  if( !hasPerm('thread.view') ) 
    return MSG_UNAUTHORIZED;

  try {
    $db = openDbConn();
    $threads = $db->query("SELECT thread.id AS id,
                         thread.fid AS fid,
                         thread.stream AS stream,
                         thread.prio AS prio,
                         thread.bumpdate AS bumpdate,
                         thread.bumpuid AS bumpuid,
                         (SELECT user.name 
                           FROM user WHERE thread.bumpuid = id) AS bumpuname,
                         post.title AS title,
                         post.datum AS datum,
                         post.uid AS uid,
                         (SELECT user.name
                           FROM user WHERE post.uid = name) AS uname,
                         post.date AS date,
                         forum.title AS ftitle
                       FROM thread
                         INNER
                           JOIN post, forum
                             ON post.stream = thread.stream
                             AND post.rank = 1
                             AND forum.id = fid
                       WHERE thread.fid = ${fid}
                       ORDER BY bumpdate DESC
                       LIMIT $start,$inc;");
    $data = $threads->fetchAll(PDO::FETCH_ASSOC);
    $threads = NULL;
    return $data;
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}

function getThreadsByUid($uid, $start, $inc) {
  if( !hasPerm('thread.view') ) 
    return MSG_UNAUTHORIZED;

  try {
    $db = openDbConn();
    
    $uid = $db->quote($uid);
    $start = $db->quote($start);
    $inc = $db->quote($inc);

    $threads = $db->query("SELECT thread.id AS id,
                                  thread.fid AS fid,
                                  thread.stream AS stream,
                                  thread.prio AS prio,
                                  thread.bumpdate AS bumpdate,
                                  thread.bumpuid AS bumpuid,
                                  (SELECT user.name 
                                    FROM user WHERE thread.bumpuid = id) AS bumpuname,
                                  post.title AS title,
                                  post.datum AS datum,
                                  post.uid AS uid,
                                  user.name AS name
                           FROM post
                             INNER JOIN thread, user
                               ON thread.stream = post.stream
                               AND post.uid = user.id
                           WHERE post.rank = 1
                             AND post.uid = ${uid} 
                           ORDER BY thread.bumpdate DESC
                           LIMIT $start, $inc;");
    $data = $threads->fetchAll(PDO::FETCH_ASSOC);
    $threads = NULL;
    return $data;
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}

?>
